// pages/login/login.js
const app = getApp();
let tool = require('../../utils/tool.js');
const httpUtils = require('../../utils/httpUtils.js');
let util = require('../../utils/util.js');

Page({

  /**
   * 页面的初始数据
   */
  data: {
    userInfo: null,
    isHaveRefer: true,
    phone: "",
    code: "",
    codetime: "获取验证码",
    isChoose: 1,
    isDaojishi: false, //是否是倒计时状态
    totleTime: 60, //倒计时时间
    timer: '', //倒计时
    type: 0,
    id:""//商品id
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let that = this
    that.setData({
      type:options.type,
    })
    that.data.type = options.type
    console.log(" that.data.type:"+that.data.type+" that.data.id:"+ that.data.id)
    if (app.globalData.referee.name) {
      this.setData({
        isHaveRefer: false
      })
    }
    that.userInfo = app.globalData.userInfo;
   
  },
  getUserfile(e) {
    let that = this;
    console.log("进入getUserProfile()")
    // 推荐使用wx.getUserProfile获取用户信息，开发者每次通过该接口获取用户个人信息均需用户确认
    // 开发者妥善保管用户快速填写的头像昵称，避免重复弹窗
  
    wx.getUserProfile({
      desc: '用于完善学员资料', // 声明获取用户个人信息后的用途，后续会展示在弹窗中，请谨慎填写
      success: (res) => {
        that.setData({
          userInfo: res.userInfo,
        })
        console.log("进入getUserProfile(vvv)" + JSON.stringify(res.userInfo))
        app.globalData.userInfo = res.useInfor;
        that.bing()
      },
      fail:(res)=>{
        console.log("failres" +  JSON.stringify(res))
      }
      
    })
  
    
  },
  login:function(e){
    let that = this;
    // var name = e.detail.value.uname;
    // var code = e.detail.value.code;
    // this.data.code = code;
    if (this.data.phone == "") {
      console.log("e.detail.value.uname888888" + e.detail.value.uname)
      wx.showToast({
        title: "请输入手机号",
        icon: 'none',
        duration: 2000
      })
      return
    }
    if (this.data.codetime == "") {
      wx.showToast({
        title: "请输入手机验证码",
        icon: 'none',
        duration: 2000
      })
      return
    }
    if (this.data.userInfo === null) {
      // this.onLoad();
      console.log("userinfo为空")
      that.getUserfile(e)
    } else {
      console.log("userinfo不为空")
      that.bing()
    }

  },
 
  bing: function () {
    wx.showLoading({
      title: '正在加载',
      mask: true
    })
    httpUtils.bind({
      openid: app.globalData.openid,
      mobile: this.data.phone,
      code: this.data.code,
      nickname: this.data.userInfo.nickName,
      avatar: this.data.userInfo.avatarUrl,
    }).then((res) => {
      app.globalData.userDetail = res.data
      app.globalData.$TOKEN=res.data.access_token
      wx.showToast({
        title: "登录成功",
        icon: 'none',
        duration: 2000
      })
      wx.navigateBack({
        delta: 1,
      })
    })
  },

  getCode: function () {
    let that = this;
    if (!that.isDaojishi) {
      that.sendCode()
    }


  },
  phoneChange: function (e) {
    let that = this;
    that.setData({
      phone: e.detail.value
    })

  },
  codeChange: function (e) {
    let that = this;
    that.setData({
      code: e.detail.value
    })

  },
  sendCode: function () {
    let that = this;
    if (that.data.phone == "") {
      wx.showToast({
        title: "请输入手机号",
        icon: 'none',
        duration: 2000
      })
      return
    }
    wx.showLoading({
      title: '正在加载',
      mask: true
    })
    httpUtils.sendmsg({
      username: that.data.phone,
      type: 1
    }).then((res) => {

      if (res.code == 1) {
        that.setData({
          codetime: "60s",
          isDaojishi: true
        })
        that.daojishi();
      }

    })
  },
  /**
   * 倒计时
   */
  daojishi: function () {
    let that = this;
    that.data.timer = setInterval(function () {

      if (that.data.isDaojishi) {
        let num = that.data.totleTime - 1;
        that.setData({
          codetime: num + "s",
          totleTime: num
        });
        //倒计时结束
        if (that.data.totleTime <= 0) {
          that.setData({
            isDaojishi: false,
            totleTime: 60,
            codetime: "获取验证码"
          })
        }
      }

    }, 1000);
  },

  useragrement: function () {
    //同意协议
    if (this.data.isChoose == 1) {
      this.data.isChoose = 0
    } else {
      this.data.isChoose = 1
    }
    this.setData({
      isChoose: this.data.isChoose
    })

  },


  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    wx.login({
      success: function (res) {
        console.log("login()")
        if (res.code) {

          app.globalData.code = res.code;
        } else {
          console.log('获取用户登录态失败！' + res.errMsg)
        }
      }
    });
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },


  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})